<?php
include "general_include.php";
$page_name='replays.php';

   unset($_SESSION['sql']);
   unset($_SESSION['sqlOrder']);
   unset($_SESSION['sqlPart']);
   unset($_SESSION['sort']);

//############## Added By Chandan
if($_SESSION['replay_game_id']!='')
    $game_id = $_SESSION['replay_game_id'];
if(isset($_REQUEST['game_id']))
{ 
	$_SESSION['replay_game_id'] = $_REQUEST['game_id'];
    $game_id = $_SESSION['replay_game_id'];
}
//############# Added By Chandan

//$game_id=$_REQUEST['game_id'];
$order_id=$_REQUEST['order_id'];
$action=$_REQUEST['action'];
$coach_id=$_REQUEST['coach_id'];
$dosearch=$_REQUEST['dosearch'];

$ladder_id = array();
$race_id = array();
$champion_id = array();
$class_id = array();
$map_id = array();
$mode_id = array();
$team_id = array();
$type_id = array();
$versus_id = array();
$numlike = array();

$server_id = $_REQUEST['server_id'];
$rating_id = $_REQUEST['rating_id'];
$availability = $_REQUEST['availability'];
$language_id = $_REQUEST['language_id'];
$vdotitle = $_REQUEST['vtitle'];


$duration = $_REQUEST['duration'];
if($duration == '')
  $duration = '0;60';
if(!empty($duration))
{
   $duration_range = explode(";",$duration);
   $min_duration = $duration_range[0];
   $max_duration = $duration_range[1];
   if($min_duration == '0' && $max_duration == '60')
    $sqlPart .='';
   else if($min_duration == '0' && $max_duration <60)
    $sqlPart .=" AND CAST(time_length as decimal) <='$max_duration'";
   else if($min_duration >'0' && $max_duration == '60')
    $sqlPart .=" AND CAST(time_length as decimal) >='$min_duration'";
   else 
     $sqlPart .=" AND time_length BETWEEN '$min_duration' AND '$max_duration'";		  
}   

$action_arr=array("list_order","list_search","list_paginate");
if(in_array($action,$action_arr))
{
	$IsProcess = 'Y';
	$smarty->assign('IsProcess',"Y");
	
}
if($IsProcess <> 'Y')
	 include "top.php";


if($game_id=='')
  $GameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE is_default='Y'";
else
  $GameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE game_id='$game_id'";  

$gamerow = $UserManagerObjAjax->GetRecords("Row",$GameSql);
$game_id=$gamerow['game_id'];
$game_name=$gamerow['game_name'];

function findvalue($table,$fld_name,$fld_value,$find_value)
	{
		 if($fld_value=='')
				return false;
				
			$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
			$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
			return $SelectType[0];
	}	


$record_per_page = !empty($record_per_page)?$record_per_page:15;
$paginationObj = new PaginationClassFrontAjax($record_per_page,3,1,'Next','Previous');


  if($dosearch=='GO')
  {
    $SLink = '';

	if(!empty($is_versus))
	{
	   $searchSql =  $searchSql." AND is_versus='Y' and ( versus='R' or versus='B' )";
	   $SLink.="&is_versus=Y&versus=R";
	} 
	if(!empty($is_race))
	{
	   $searchSql =  $searchSql." AND is_race='Y' and ( race='R' or race='B' )";
	   $SLink.="&is_race=Y&race=R";
	} 

	if(!empty($is_ladder))
	{
	   $searchSql =  $searchSql." AND is_ladder='Y' and ( ladder='R' or ladder='B' )";
	   $SLink.="&is_ladder=Y&ladder=R";
	} 

	if(!empty($is_champion))
	{
	   $searchSql =  $searchSql." AND is_champion='Y' and ( champion='R' or champion='B' )";
	   $SLink.="&is_champion=Y&champion=R";
	} 
	if(!empty($is_class))
	{
	   $searchSql =  $searchSql." AND is_class='Y' and ( class='R' or class='B' )";
	   $SLink.="&is_class=Y&class=R";
	} 
	if(!empty($is_map))
	{
	   $searchSql =  $searchSql." AND is_map='Y' and ( map='R' or map='B' )";
	   $SLink.="&is_map=Y&map=R";
	}

	$SearchLink="dosearch=GO".$SLink;
  } 
  if(!empty($vdotitle))
  {
    $vdoUser = "SELECT user_id from ".TABLEPREFIX."_user where name like ='$vdotitle%'";
	$vdoUserArr = $UserManagerObjAjax->GetRecords("All",$vdoUser);
    for($x=0;$x<count($vdoUserArr);$x++)
	{ 
	  $uid[] = $vdoUserArr[$x]['user_id'];
	}
	$uids= implode(",",$uid);
    $searchSql= $searchSql." AND (video_title like '$vdotitle%' OR video_description like '%$vdotitle%' OR user_id IN($uids))";
  }


 $SelectreplaySql = "SELECT *,date_format(".date_added.",'%M  %d, %Y at %h %i %p') as video_date FROM ".TABLEPREFIX."_video WHERE game_id='$game_id'".$searchSql;
 $Arr = $UserManagerObjAjax->GetRecords("All",$SelectreplaySql);
 $totRec = count($Arr);
 
 $pagination_arr = $paginationObj->PaginationFrontAjaxALL('PaginationSearch',$SelectreplaySql,$page_name.'?'.$catLink);
 $ReplayArr = $UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);
 $NumReplay = count($ReplayArr);
    for($i=0;$i<$NumReplay;$i++)
	{
		$ReplayArr[$i]['like']='N';
		
		$ReplayArr[$i]['user'] = findvalue(TABLEPREFIX."_user","user_id",$ReplayArr[$i]['user_id'],"name");
		$totstar='';
		for($x=1;$x<=$ReplayArr[$i]['overall_rating'];$x++)
		{
		  $totstar.="<img src='images/starb.gif' alt='' border='0' />";
		}
		$istar=5 - $ReplayArr[$i]['overall_rating']; 
		for($y=1;$y<=$istar;$y++)
		{
		  $totstar.="<img src='images/starg.gif' alt='' border='0' />";
		}
		$ReplayArr[$i]['star']=$totstar;
		$viewCount = "SELECT count(*) FROM ".TABLEPREFIX."_video_view_count WHERE video_id =".$ReplayArr[$i]['video_id'];
		$viewArr = $UserManagerObjAjax->GetRecords("Row",$viewCount);
		$ReplayArr[$i]['view_count'] = $viewArr[0];
		
		
		/*$LikeSql = "SELECT * FROM ".TABLEPREFIX."_user_like WHERE user_id=".$ReplayArr[$i]['user_id']." AND user_type_id=".$_SESSION['user_type']." AND selected_by=".$_SESSION['user_id']." AND video_id=".$ReplayArr[$i]['video_id'] ;
		$LikeArr = $UserManagerObjAjax->GetRecords('Row',$LikeSql);
		if(count($LikeArr)>0) $ReplayArr[$i]['like']='Y';*/
		
		$LikeNumSql = "SELECT count(*) AS numlike FROM ".TABLEPREFIX."_user_like WHERE selected_for='L' AND video_id=".$ReplayArr[$i]['video_id'] ;
		$LikeArrNum = $UserManagerObjAjax->GetRecords('Row',$LikeNumSql);
		
		$ReplayArr[$i]['numlike']=$LikeArrNum['numlike'];
		
		$UnlikeNumSql = "SELECT count(*) AS numunlike FROM ".TABLEPREFIX."_user_like WHERE selected_for='D' AND  video_id=".$ReplayArr[$i]['video_id'] ;
		$UnlikeArrNum = $UserManagerObjAjax->GetRecords('Row',$UnlikeNumSql);
		
		$ReplayArr[$i]['numunlike']=$UnlikeArrNum['numunlike'];
		
		//$numlike[$i]=$ReplayArr[$i]['numlike'];
		
	}
	
	//print_r($ReplayArr);




$gameSql = "SELECT game_id,game_name FROM ".TABLEPREFIX."_game where is_active='Y' ORDER BY game_name";
$GameArr = $UserManagerObjAjax->HtmlOptionArrayCreate($gameSql);


$replaySql="SELECT * FROM ".TABLEPREFIX."_video WHERE game_id='$game_id'".$searchSql.$sqlPart;
//$Arr has total records


/* find the ladder of selected game */

if($gamerow['is_ladder']=='Y' and ( $gamerow['ladder']=='R' or $gamerow['ladder']=='B' ) )
{
	$is_ladder = 'Y';
	$Selectladder="SELECT * FROM ".TABLEPREFIX."_game_ladder WHERE game_id='$game_id' and is_active='Y' order by ladder_order";
	$LadderArr=$UserManagerObjAjax->GetRecords("All",$Selectladder);
	$Numladder=count($LadderArr);
	for($gl=0;$gl<$Numladder;$gl++)
	{
		$query=$replaySql." and ladder_id=".$LadderArr[$gl]['ladder_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$LadderArr[$gl]['countrow']=count($replayCount); 
	}
}

/* find the race of selected game */
if( $gamerow['is_race']=='Y' and ( $gamerow['race']=='R' or $gamerow['race']=='B' ) )
{
	$is_race = 'Y';
	$Selectrace="SELECT * FROM ".TABLEPREFIX."_game_race WHERE game_id='$game_id' and is_active='Y'";
	$RaceArr=$UserManagerObjAjax->GetRecords("All",$Selectrace);
	$Numrace=count($RaceArr);
	for($gr=0;$gr<$Numrace;$gr++)
	{
		$query=$replaySql." and race_id=".$RaceArr[$gr]['race_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$RaceArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Champion of selected game */
if( $gamerow['is_champion']=='Y' and ( $gamerow['champion']=='R' or $gamerow['champion']=='B' ) )
{
	$is_champion = 'Y';
	$SelectChampion="SELECT * FROM ".TABLEPREFIX."_game_champion WHERE game_id='$game_id' and is_active='Y'";
	$ChampionArr=$UserManagerObjAjax->GetRecords("All",$SelectChampion);
	$NumChampion=count($ChampionArr);
	for($gr=0;$gr<$NumChampion;$gr++)
	{
		$query=$replaySql." and champion_id=".$ChampionArr[$gr]['champion_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$ChampionArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Class of selected game */
if( $gamerow['is_class']=='Y' and ( $gamerow['class']=='R' or $gamerow['class']=='B' ) )
{
	$is_class = 'Y';
	$SelectClass="SELECT * FROM ".TABLEPREFIX."_game_class WHERE game_id='$game_id' and is_active='Y'";
	$ClassArr=$UserManagerObjAjax->GetRecords("All",$SelectClass);
	$NumClass=count($ClassArr);
	for($gr=0;$gr<$NumClass;$gr++)
	{
		$query=$replaySql." and class_id=".$ClassArr[$gr]['class_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$ClassArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Map of selected game */
if( $gamerow['is_map']=='Y' and ( $gamerow['map']=='R' or $gamerow['map']=='B' ) )
{
	$is_map = 'Y';
	$SelectMap="SELECT * FROM ".TABLEPREFIX."_game_map WHERE game_id='$game_id' and is_active='Y'";
	$MapArr=$UserManagerObjAjax->GetRecords("All",$SelectMap);
	$NumMap=count($MapArr);
	for($gr=0;$gr<$NumMap;$gr++)
	{
		$query=$replaySql." and map_id=".$MapArr[$gr]['map_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$MapArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Mode of selected game */
if( $gamerow['is_mode']=='Y' and ( $gamerow['mode']=='R' or $gamerow['mode']=='B' ) )
{
	$is_mode = 'Y';
	$SelectMode="SELECT * FROM ".TABLEPREFIX."_game_mode WHERE game_id='$game_id' and is_active='Y'";
	$ModeArr=$UserManagerObjAjax->GetRecords("All",$SelectMode);
	$NumMode=count($ModeArr);
	for($gr=0;$gr<$NumMode;$gr++)
	{
		$query=$replaySql." and mode_id=".$ModeArr[$gr]['mode_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$ModeArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Team of selected game */
if( $gamerow['is_team']=='Y' and ( $gamerow['team']=='R' or $gamerow['team']=='B' ) )
{
	$is_team = 'Y';
	$SelectTeam="SELECT * FROM ".TABLEPREFIX."_game_team WHERE game_id='$game_id' and is_active='Y'";
	$TeamArr=$UserManagerObjAjax->GetRecords("All",$SelectTeam);
	$NumTeam=count($TeamArr);
	for($gr=0;$gr<$NumTeam;$gr++)
	{
		$query=$replaySql." and team_id=".$TeamArr[$gr]['team_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$TeamArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Type of selected game */
if( $gamerow['is_type']=='Y' and ( $gamerow['type']=='R' or $gamerow['type']=='B' ) )
{
	$is_type = 'Y';
	$SelectType="SELECT * FROM ".TABLEPREFIX."_game_type WHERE game_id='$game_id' and is_active='Y'";
	$TypeArr=$UserManagerObjAjax->GetRecords("All",$SelectType);
	$NumType=count($TypeArr);
	for($gr=0;$gr<$NumType;$gr++)
	{
		$query=$replaySql." and type_id=".$TypeArr[$gr]['type_id'];
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$TypeArr[$gr]['countrow']=count($replayCount); 
	}
}

/* find the Versus of selected game */
if( $gamerow['is_versus']=='Y' and ( $gamerow['versus']=='R' or $gamerow['versus']=='B' ) )
{
	$is_versus = 'Y';
	$SelectVersus="SELECT * FROM ".TABLEPREFIX."_game_versus WHERE game_id='$game_id' and is_active='Y'";
	$VersusArr=$UserManagerObjAjax->GetRecords("All",$SelectVersus);
	$NumVersus=count($VersusArr);
	for($gr=0;$gr<$NumVersus;$gr++)
	{
		$query=$replaySql." and FIND_IN_SET(".$VersusArr[$gr]['versus_id'].",versus_id)";
		$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
		$VersusArr[$gr]['countrow']=count($replayCount); 
	}
}
/* for star rating */
for($s=1,$m=0;$s<=5;$s++) 
{
    $query=$replaySql." and overall_rating=".$s;
	$replayCount=$UserManagerObjAjax->GetRecords("All",$query); 
	
	if(count($replayCount)>0)
	{
	  $StarArr[$m]['countrow']=count($replayCount);
	  $StarArr[$m]['star']=$s;
	  $m++;
	}  
	
}

$smarty->assign("GameArr",$GameArr);
$smarty->assign('game_id',$game_id);
$smarty->assign('coach_id',$coach_id);
$smarty->assign('game_name',$game_name);
$smarty->assign('coachArr',$coachArr);
$smarty->assign("NumReplay",$NumReplay);
$smarty->assign("totRec",$totRec);
$smarty->assign('duration',$duration);

$smarty->assign('numlike',$numlike);

$smarty->assign('Numladder',$Numladder);
$smarty->assign('LadderArr',$LadderArr);
$smarty->assign('Numrace',$Numrace);
$smarty->assign('RaceArr',$RaceArr);
$smarty->assign('NumChampion',$NumChampion);
$smarty->assign('ChampionArr',$ChampionArr);
$smarty->assign('NumClass',$NumClass);
$smarty->assign('ClassArr',$ClassArr);
$smarty->assign('NumMap',$NumMap);
$smarty->assign('MapArr',$MapArr);
$smarty->assign('NumMode',$NumMode);
$smarty->assign('ModeArr',$ModeArr);
$smarty->assign('NumTeam',$NumTeam);
$smarty->assign('TeamArr',$TeamArr);
$smarty->assign('NumType',$NumType);
$smarty->assign('TypeArr',$TypeArr);
$smarty->assign('NumVersus',$NumVersus);
$smarty->assign('VersusArr',$VersusArr);
$smarty->assign('vdotitle',$vdotitle);
$smarty->assign('StarArr',$StarArr);
$smarty->assign('SearchLink',$SearchLink);
$smarty->assign('SLink',$SLink);
$smarty->assign("ReplayArr",$ReplayArr);
$smarty->assign('pagination_arr',$pagination_arr);
$smarty->assign('page_name',$page_name);	
$smarty->register_modifier('in_array', 'in_array');
if($IsProcess <> 'Y')
	 include "replays_leftpanel.php";
$smarty->display('replays.tpl');
if($IsProcess <> 'Y')
	 include "footer.php";
?>